<%@ page import="coop.bancocredicoop.proyectos.gd.controller.buscarDocumento.Main" %>
<%@ page import="coop.bancocredicoop.proyectos.gd.domain.Document" %>
<%@ page import="java.util.List" %>

<%@ include file="../header.jsp" %>



<%
	String paramCustomerDescription = null;
	
	new Main().buscarDocumento(request);
	
	List<String> listError = (List<String>) request.getAttribute("listError");
	List<Document> listDocument = (List<Document>) request.getAttribute("listDocument");
	
%>

		<script>
			function clearCustomer() {
				$('#customerId').val('')
				$('#customerDescription').val('')
				$('#customer').html('')
				
				return false
			}

		    function previewDocument(documentId) {
		        console.debug(documentId);
		        var baseUrl = '<%=request.getContextPath()%>/documento/abrirDesdeRepositorio';
		        var url = baseUrl + '?documentId=' + documentId;
		        var previewer = GD.DocumentPreviewer();
		        previewer.previewDocument(url, 'Documento');
		    }

			function loadDocumentDetail(documentId) {
				$('#detalleDocumentoModal').load("<%=request.getContextPath()%>/jsp/consultaDocumento/index.jsp",
						"id=" + documentId)
				$('#detalleDocumentoModal').modal('show')

				return false
			}

			function cambiarPagina(pageOffset) {
				$("#offset").val(pageOffset)
				$("#botonBuscar").click()

				return false
			}

			function prevPagina() {
				<%--var offset = ${params.offset ? Integer.valueOf(params.offset) - 20 : 0}--%>

				$("#offset").val(offset)
				$("#botonBuscar").click()

				return false
			}
		</script>


		<div class="container">
		<div class="main-panel">
		<div class="panel panel-default">
		<div class="panel-heading"><h2>Buscar Documento</h2></div>
		<div class="panel-body">

		<form method="post">
			<div class="form-group">
				<label for="grupoDocumental">Grupo Documental</label>
				<select name="grupoDocumental" multiple="multiple" class="form-control">
					<% if (((RBAC)session.getAttribute("rbac")).tienePermiso("GD2_CONSULTA")) { %>
						<option selected="selected" value="GD2">Grupo Documental 2</option>
					<% } %>
				</select> 
			</div>

			<div class="form-group">
				<label for="customer">Asociado</label>
				<input type="hidden" id="customerId" name="customerId" value="<%=request.getParameter("customerId") != null ? request.getParameter("customerId") : ""%>" />
				<input type="hidden" id="customerDescription" name="customerDescription" value="<%=request.getParameter("customerDescription") != null ? request.getParameter("customerDescription") : ""%>" />
				<div class="row">
				<div class="col-xs-9">
					<div class="input-group">
						<% if (request.getParameter("customerDescription") != null && !request.getParameter("customerDescription").trim().equals("")) { %>
							<div class="form-control" id="customer"><%=request.getParameter("customerDescription") %></div>
						<% } else { %>
							<div class="form-control" id="customer"><span style="color:#aaa">Asociado</span></div>
						<% } %>
						
						<span class="input-group-btn">
							<a href="#" onclick="return clearCustomer()" class="btn btn-default" title="Borrar"><span class="glyphicon glyphicon-remove"></span></a>
						</span>
					</div>
				</div>
				<div class="col-xs-3">
					<a data-toggle="modal" href="../buscarCliente/index.jsp" data-target="#buscarClienteModal" class="btn btn-default">Buscar Asociado</a>
				</div>
				</div>
			</div>

			<div class="form-group">
				<label for="cajaId">Caja</label>
				<input id="cajaId" type="text" class="form-control" name="cajaId" placeholder="Caja" value="<%=request.getParameter("cajaId") != null ? request.getParameter("cajaId") : ""%>" />
			</div>

			<div class="form-group">
				<input type="submit" name="buscarDocumento" value="Buscar" class="btn btn-primary" />
			</div>

			<% if (listDocument != null && !listDocument.isEmpty()) { %>
			<table class="table table-striped">
			<thead>
					<tr>

						<th>Asociado</th>
						<th>Tipo de Documento</th>
						<th>Caja AdeA</th>
						<th>Versi&oacute;n</th>
						<th></th>

					</tr>
				</thead>
				<tbody>
				<% for (Document document : listDocument) { %>
					<tr>
						<td><%=document.getCustomer().getIdPersona() %> (<%=document.getCustomer().getName() %>, CUIT: <%=document.getCustomer().getCuit() %>)</td>

						<td><%=document.getDocumentType().getName() %></td>

						<td><%=document.getBox().getCajaId() %></td>

						<td><%=document.getRealVersion() %></td>

						<td class="col-md-2">
							<button type="button" class="btn btn-default" onclick="previewDocument('<%=document.getId() %>')"><span class="glyphicon glyphicon-eye-open"></span></button>
							<a href="#" onclick="return loadDocumentDetail(<%=document.getId() %>)" class="btn btn-default"><span class="glyphicon glyphicon-info-sign"></span></a>
						<%--
							<p>
								<a href="controller:'consultaDocumentos', id:document.id)}" class="btn-sm btn-info">Ver</a>
								<a data-toggle="modal" href="controller:'consultaDocumentos', action:'modalShow', id:document.id)}" data-target="#detalleDocumentoModal"  class="btn-sm btn-info">Datos</a>
							</p>
 --%>
						</td>
					</tr>
				<% } %>
				</tbody>
			</table>

				<input type="hidden" id="offset" name="offset" />
				<%--ul class="pagination">
					<% for (int i = 0; i <= (listDocument.size() - 1) / 5; i++) { --%>
						<%-- if (request.getParameter("offset") == null && i == 0 ||
								Integer.valueOf(request.getParameter("offset")).intValue() / 5 == i) { --%>
							<%--li><a class="active" href="#" onclick="return cambiarPagina(<%=String.valueOf(i * 5) %>)"><%=String.valueOf(i + 1) %></a></li%-->
						<% } else { --%>
							<%--li><a href="#" onclick="return cambiarPagina(<%=i * 5 %>)"><%=i + 1 %></a></li>
						<%-- } --%>
					<%-- } --%>
					<%--li class="active"><a href="#" onclick="return prevPagina()">&laquo;</a></li--%>
					<%--li><a href="#">&raquo;</a></li--%>
				<%--/ul--%>

			<% } %>

			<% if (listError != null && !listError.isEmpty()) { %>
				<div class="alert alert-danger">
					<ul class="errors">
						<% for (String error : listError) { %>
							<li><%=error %></li>
						<% } %>
					</ul>
				</div>
			<% } %>

		</form>

		</div>
		</div>
		</div>
		</div>

		<div class="modal fade" id="buscarClienteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>
		<div class="modal fade" id="detalleDocumentoModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>

<%@ include file="../footer.jsp" %>